#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
map<char, pii> mp;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) {
    char c;
    int x, y;
    cin >> c >> x >> y;
    mp[c] = pii(x, y);
  }
  string expr;
  while (cin >> expr) {
    stack<pii> st;
    ll ans = 0;
    for (char c : expr) {
      if (c == '(')
        continue;
      else if (c == ')') {
        pii y = st.top();
        st.pop();
        pii x = st.top();
        st.pop();
        if (x.second != y.first) {
          ans = -1;  // error
          break;
        }
        ans += 1ll * x.first * x.second * y.second;
        pii z = pii(x.first, y.second);
        st.push(z);
      } else
        st.push(mp[c]);
    }
    if (ans < 0)
      cout << "error" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}